package com.vr.core.system.system.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.jwt.JWTUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.vr.core.system.system.controller.vo.CurrentUserInfo;
import com.vr.core.system.system.controller.vo.LoginParams;
import com.vr.core.system.userInfo.service.SysUserService;
import com.vr.core.system.userInfo.userInfo.SysUser;
import com.vr.tool.api.R;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Description TODO
 * @Author kenson
 * @Date 2024/12/8 17:03
 */

@RequestMapping("/login")
@RestController
@AllArgsConstructor
public class LoginController {

    private final SysUserService userService;
    private final String secret ="Kio12LiIl1lzz09od&uiy^uhi&";


    // 用户凭据（账号密码登录）
    @PostMapping("/userCredentials")
    public R login(@RequestBody LoginParams loginParams){
        SysUser one = userService.getOne(new LambdaQueryWrapper<SysUser>().eq(SysUser::getUsername, loginParams.getUsername()).eq(SysUser::getPassword, loginParams.getPassword()).last("LIMIT 1"));
        if(one != null){
            String token = JWTUtil.createToken(BeanUtil.beanToMap(one), secret.getBytes());
            return R.ok(new CurrentUserInfo(one.getUsername(),token));

        }else {
            return R.fail("登录失败!");
        }



    }

    @PostMapping("/outLogin")
    public R outLogin(){



        return R.ok("");
    }
}
